﻿function Get-TSMAdministrator {    
    <#
        .SYNOPSIS
            Get TSM administrators
                    
        .DESCRIPTION
            The Get-TSMAdministrator returns registered administrators

        .PARAMETER Admin
            The admin to search

        .PARAMETER Class
            The privelage class to search

        .PARAMETER Alert
            Query if alert is on or off

        .EXAMPLE
            Get-TSMAdministrator

        .NOTES
            Contributors:  David Seibel
            Last Revision Date: 09/02/2015

    #>
    [CmdletBinding()]
    Param(
        [Parameter(Position=0)]
        $Admin,

        [Parameter(Position=1)]
        [ValidateSet("System","Policy","Storage","Operator","Node")]
        $Class,

        [ValidateSet("Yes","No")]
        $Alert
    )
    
    $whereProps = @()
    $whereProps += [PSCustomObject]@{ "Column" = "ADMIN_NAME";  "Operator" = "LIKE"; "Value" = $Admin }
    $whereProps += [PSCustomObject]@{ "Column" = "ALERT_EMAIL"; "Operator" = "LIKE"; "Value" = $Alert }

    switch ($Classes) {
        "System"   { $whereProps += [PSCustomObject]@{ "Column" = "SYSTEM_PRIV";   "Operator" = "=";  "Value" = "YES" } }
        "Policy"   { $whereProps += [PSCustomObject]@{ "Column" = "POLICY_PRIV";   "Operator" = "=";  "Value" = "YES" } }
        "Storage"  { $whereProps += [PSCustomObject]@{ "Column" = "STORAGE_PRIV";  "Operator" = "=";  "Value" = "YES" } }
        "Operator" { $whereProps += [PSCustomObject]@{ "Column" = "OPERATOR_PRIV"; "Operator" = "=";  "Value" = "YES" } }
        "Node"     { $whereProps += [PSCustomObject]@{ "Column" = "CLIENT_ACCESS"; "Operator" = "<>"; "Value" = "" } 
                     $whereProps += [PSCustomObject]@{ "Column" = "CLIENT_OWNER";  "Operator" = "<>"; "Value" = "" }
        }
    }


    $query = Format-TSMQuery -Table "ADMINS" -Where $whereProps -OrderBy "ADMIN_NAME"
    $result = Invoke-TSMCommand -Command $query

    $defaultDisplaySet = 'ADMIN_NAME','LASTACC_TIME','PWSET_TIME','LOCKED'
    $typeDefinitions = @{
                        "PWSET_TIME"          = [DateTime]
                        "LASTACC_TIME"        = [DateTime]
                        "REG_TIME"            = [DateTime]
                        "INVALID_PW_COUNT"    = [Int32]
                        "PASSEXP"             = [Int32]
                        }

    return Format-TSMData -Data $result -TypeDefinitions $typeDefinitions -DefaultDisplaySet $defaultDisplaySet
}